(require 'package)
;; Any add to list for package-archives (to add marmalade or melpa) goes here
(add-to-list 'package-archives 
	     '("MELPA" .
	       "http://melpa.milkbox.net/packages/"))
(package-initialize)


(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-when-compile
  (require 'use-package))
(setq use-package-always-ensure t)

;; avy is a GNU Emacs package for jumping to visible text using a char-based decision tree.
;; https://github.com/abo-abo/avy/tree/3bf83140fad4c28f2dc4c7107b9d8fef84d17cb9
(use-package avy
  :ensure t
  :commands (avy-goto-word-0
	     avy-goto-line
	     avy-resume
	     avy-copy-line
	     avy-copy-region)
  :bind (("C-c a g w" . #'avy-goto-word-0)
	 ("C-c a g s" . #'avy-goto-subword-0)
	 ("C-c a g c" . #'avy-goto-char-timer)
	 ("C-c a g l" . #'avy-goto-line)
	 ("C-c a c l" . #'avy-copy-line)
	 ("C-c a c r" . #'avy-copy-region)
	 ("C-c a C-j" . #'avy-resume)
	 ("C-c a s l" . #'avy-kill-ring-save-whole-line)
	 ("C-c a s r" . #'avy-kill-ring-save-region)
	 ("C-c a m l" . #'avy-move-line)
	 ("C-c a m r" . #'avy-move-region)
	 ("M-g c"     . #'avy-goto-char)
	 ("M-g l"     . #'avy-goto-line)
	 ("M-g w"     . #'avy-goto-word-1)
	 ("M-z"       . #'avy-zap-to-char-dwim)
	 :map isearch-mode-map
	 ("C-'" . #'avy-isearch)))

(use-package avy-zap
  :ensure t
  :commands (avy-zap-to-char-dwim
	     avy-zap-up-to-char-dwim)
  :bind (("C-c a z t" . #'avy-zap-to-char-dwim)
	 ("C-c a z u" . #'avy-zap-up-to-char-dwim)))

(use-package auto-org-md)

(use-package markdown-mode
  :ensure t
  :mode (("README\\.md\\'" . gfm-mode)
	 ("\\.md\\'" . markdown-mode)
	 ("\\.markdown\\'" . markdown-mode))
  :init (setq markdown-command "multimarkdown"))

(use-package deft
  :defer t
  :delight (deft-mode "D")
  :commands (deft)
  :config (setq deft-directory "~/documents"
		deft-extensions '("org" "md")
		deft-default-extension "org"
		deft-text-mode 'org-mode))

(setq custom-file "~/.emacs.d/custom.el")
(unless (file-exists-p custom-file)
  (write-region "" nil custom-file))
;;; Load custom file. Don't hide errors. Hide success message
(load custom-file nil t)

(menu-bar-mode -1) ;; don't need the menu bar especially in terminal
(load-theme 'wombat) ;; but you want some color
(defalias 'yes-or-no-p 'y-or-n-p) ;; change yes or no to y or n
(show-paren-mode 1) ;; show matching parens

(defvar --backup-directory (concat user-emacs-directory "backups"))
(if (not (file-exists-p --backup-directory))
    (make-directory --backup-directory t))
(setq backup-directory-alist `(("." . ,--backup-directory)))
(setq make-backup-files t               ; backup of a file the first time it is saved.
      backup-by-copying t               ; don't clobber symlinks
      version-control t                 ; version numbers for backup files
      delete-old-versions t             ; delete excess backup files silently
      delete-by-moving-to-trash t
      create-lockfiles nil		; don't litter the system with lockfiles
      kept-old-versions 6               ; oldest versions to keep when a new numbered backup is made (default: 2)
      kept-new-versions 9               ; newest versions to keep when a new numbered backup is made (default: 2)
      auto-save-default t               ; auto-save every buffer that visits a file
      auto-save-timeout 20              ; number of seconds idle time before auto-save (default: 30)
      auto-save-interval 200            ; number of keystrokes between auto-saves (default: 300)
      )

(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(setq ido-use-filename-at-point 'guess)
(setq ido-create-new-buffer 'always)
(setq ido-file-extensions-order '(".org" ".md" ".txt" ".py" ".emacs" ".xml" ".el" ".ini" ".cfg" ".cnf"))
(setq linum-format "%4d ")
(ido-mode 1)

(defun tilde/prog-mode-hook ()
  (linum-mode t))

(defun tilde/reload-init ()
  (interactive)
  (load-file "~/.emacs.d/init.el"))

(defun tilde/find-init ()
  (interactive)
  (find-file (concat user-emacs-directory "/init.el")))

(add-hook 'prog-mode-hook #'tilde/prog-mode-hook)

(defvar bb-bin "~/.local/bin/bb")
(defvar bb-post "post " "bb post action")
(defvar bb-edit "edit ")
(defvar bb-delete "delete ")
(defvar bb-rebuild "rebuild ")
(defvar bb-reset "reset ")
(defvar bb-list "list ")
(defvar bb-tags "tags ")
(defvar bb-html "-html " "Force bb to use html rather than markdown")
(defvar bb-rename-or-list-all-tags-n "-n ")
(defvar bb-use-full-html-f "-f ")

(defun bash-blog (&optional action  flags filename)
  "BashBlog wrapper "
  (async-shell-command (concat bb-bin " " action flags filename) "*Bash-Blog Output*" "*Bash-Blog Error*")
  )

(defun show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name))
  (kill-new (file-truename buffer-file-name))
  )

(defun post-markdown-to-bash-blog ()
  "Let bash-blog do all the work, just don't edit the file. Why isn't this an option?"
  (interactive)
  (bash-blog bb-post nil (buffer-file-name))
  (switch-to-buffer "*Bash-Blog Output*"))

(global-set-key (kbd "C-x C-b") #'ibuffer) ; rebind with ibuffer
(global-set-key (kbd "M-,") #'beginning-of-buffer)
(global-set-key (kbd "M-.") #'end-of-buffer)
(global-set-key (kbd "C-c f r") #'tilde/reload-init)
(global-set-key (kbd "C-c f i") #'tilde/find-init)
(global-set-key (kbd "M-/") #'hippie-expand)
(global-set-key (kbd "M-a") #'beginning-of-visual-line)
(global-set-key (kbd "M-e") #'end-of-visual-line)
(global-set-key (kbd "C-c t r") #'ruler-mode)
(global-set-key (kbd "C-c p m") #'post-markdown-to-bash-blog)
(global-set-key (kbd "C-c z") 'show-file-name)
(global-set-key (kbd "C-c d") #'deft)
